package cn.controller

import com.google.gson.Gson
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.ResponseBody
import java.time.Clock
import java.time.LocalDate

@Controller
class HomeController {
    private var logger: Logger = LoggerFactory.getLogger(HomeController::class.java)

    @RequestMapping("/index")
    fun redirectToHome(model: Model): String {
        logger.info("redirect to home")
        model.addAttribute("current_time", LocalDate.now(Clock.systemDefaultZone()).toString())
        return "Home"
    }

    @RequestMapping(value="/rest/{count}", method= [RequestMethod.GET])
    @ResponseBody
    fun indOwner(@PathVariable count : String): String{
        var obj = Array<Entry>(count.toInt(), {i -> Entry("$i: Hello, World!")})
        val gson = Gson()
        return gson.toJson(obj)
    }
}

data class Entry(val message: String)